package com.mjh.zhujianju.service.base.impl;

import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.mjh.zhujianju.dao.base.HitsMapper;
import com.mjh.zhujianju.model.base.HitsEO;
import com.mjh.zhujianju.service.base.HitsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author liuyang
 * @Description: 阅读量
 * @date 2018/8/11 17:54
 */
@Service
public class HitsServiceImpl extends BaseServiceImpl<HitsEO> implements HitsService {

    @Autowired
    private HitsMapper hitsMapper;

    public HitsServiceImpl(HitsMapper hitsMapper) {
        super(hitsMapper);
    }

    @Override
    public Long queryHitsTotal(String type, Long businessId) {
        HitsEO hitsEO = new HitsEO();
        hitsEO.setType(type);
        hitsEO.setBusinessId(businessId);
        hitsEO = hitsMapper.queryHitsTotal(hitsEO);
        if (hitsEO == null){
            return 0L;
        }
        return hitsEO.getNumber();
    }

    @Override
    public void updateNumber(String type, Long businessId) {
        HitsEO hitsEO = new HitsEO();
        hitsEO.setType(type);
        hitsEO.setBusinessId(businessId);
        HitsEO hits = hitsMapper.queryHitsTotal(hitsEO);
        if (hits == null){
            hitsEO.setNumber(1L);
            hitsEO.setCreateTime(new Date());
            hitsMapper.insertSelective(hitsEO);
        }else {
            hitsMapper.updateNumber(hitsEO);
        }
    }
}
